package com.siwei.mes.service.experiment.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.ShxhSynchronizedata;
import com.siwei.mes.entity.UploadLaboratoryEntity;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ExperimentWaybill;
import com.siwei.mes.entity.sync.tc.Waybill;
import com.siwei.mes.mapper.ShxhSynchronizedataMapper;
import com.siwei.mes.mapper.experiment.ExperimentMapper;
import com.siwei.mes.mapper.experiment.ExperimentWaybillMapper;
import com.siwei.mes.mapper.syncTc.WaybillMapper;
import com.siwei.mes.service.experiment.ExperimentWaybillService;
import com.siwei.mes.sync.config.SyncProcessorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账与运单关系(ExperimentWaybill)表服务实现类
 *
 * @author makejava
 * @since 2024-05-27 21:37:50
 */
@Slf4j
@Service
public class ExperimentWaybillServiceImpl implements ExperimentWaybillService {
    @Resource
    private ExperimentWaybillMapper experimentWaybillMapper;
    @Resource
    private ExperimentMapper experimentMapper;
    @Resource
    private SyncProcessorService syncProcessorService;
    @Resource
    private ShxhSynchronizedataMapper shxhSynchronizedataMapper;
    @Resource
    private WaybillMapper waybillMapper;

    @Override
    public Long create(ExperimentWaybill entity) {
        experimentWaybillMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ExperimentWaybill entity) {
        ExperimentWaybill experimentWaybill = experimentWaybillMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experimentWaybill == null, "试验台账与运单关系信息不存在");
        experimentWaybillMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ExperimentWaybill experimentWaybill = experimentWaybillMapper.selectByPrimaryKey(id);
        assertTrue(experimentWaybill == null, "试验台账与运单关系信息不存在");
        experimentWaybillMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ExperimentWaybill> getPage(PageQuery<ExperimentWaybill, ExperimentWaybill> query) {
        ExperimentWaybill searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentWaybillMapper.getList(searchParam));
    }

    @Override
    public ExperimentWaybill loadById(Long id) {
        return experimentWaybillMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ExperimentWaybill> getAll(ExperimentWaybill entity) {
        return experimentWaybillMapper.getList(entity);
    }

    @Override
    public List<Waybill> selectByExperimentId(Long id) {
        return experimentWaybillMapper.selectByExperimentId(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByExperimentIdAndWaybillId(Long experimentId, Long waybillId) {
        log.info("开始删除运单信息，试验委托id:【{}】,运单id:【{}】", experimentId, waybillId);

        Waybill waybill = waybillMapper.selectByWid(waybillId);
        assertTrue(waybill == null, "运单不存在");

        experimentWaybillMapper.deleteByExperimentIdAndWaybillId(experimentId, waybill.getId());
        /**修改这个试验台账的代表数量 == 所有跟这个试验台账相关的运单签收数量之和*/
        List<Waybill> waybillList = experimentWaybillMapper.selectByExperimentId(experimentId);
        Double behalfNumberSum = null;
        if (!waybillList.isEmpty()) {
            behalfNumberSum = waybillList.stream().map(Waybill::getBehalfNumber).reduce((double) 0, Double::sum);
            Experiment experiment = new Experiment();
            experiment.setId(experimentId);
            experiment.setBehalfNumber(behalfNumberSum);
            experiment.init(false);
            experimentMapper.updateByPrimaryKeySelective(experiment);
        }

        /**这里判断是否已经上传到协会，如果没有上传到协会就不推送*/
        ShxhSynchronizedata shxhSynchronizedata = shxhSynchronizedataMapper.selectByExperimentIdOne(experimentId);
        if (shxhSynchronizedata != null && shxhSynchronizedata.getSampleId() != null) {

            log.info("删除原材料试验委托和运单关系，推送到协会处理代表数量。");
            List<String> experimentIdList = new ArrayList<>();
            experimentIdList.add(experimentId.toString());
            /**推送到协会*/
            syncProcessorService.submitRequest(new UploadLaboratoryEntity().setExperimentId(experimentIdList).setUploadState(0));
        }
    }
}
